<?php
/* 科目二 科目三 练车场地管理
 *  */
namespace Admin\Controller;
class SiteController extends ComController{
	//练成场地列表
	public function  index(){
		$p= intval($_GET['p'])?$_GET['p']:1;
		$pagesize = 15;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$sc_id = session('uid');
		//var_dump($sc_id);die;
		//$sc_id=2;
		$site=M('site');
		$where = "school_id = $sc_id   and  status = 1";
		$count = $site->where($where)->count();
		$res = $site->where($where)->limit($offset . ',' . $pagesize)->select();
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();


		/* dump($res);
		dump($site->getLastSql());
		exit; */
		$this->assign('list', $res);
		$this->assign('page', $page);
		$this->display();
		
	}
	public function add(){
		if(empty($_POST)){
			$this->show();
		}else{
			
			$data['course']= I('post.course','','intval');
			if(empty($data['course'])){$this->error('请选择场地类型');die;}
			$data['site'] = I('post.site','','strip_tags');
			if(empty($data['site'])){$this->error('请填写场地名称');die;}
			$data['school_id']=session('uid');
			if(empty($data['school_id'])){$this->error('您尚未登录，请登录',U('Login/index'));die;}
			$data['add_time']=time();
			$site =M('site'); 
			$res = $site->data($data)->add();
			if($res){
				$this->success('添加成功',U('Site/index'));
			}else{
				$data['msg']='添加失败,请重新添加';
				$this->assign('data',$data);
				$this->display();
			}
		}
		
	}





	/**
	 * 场地修改
	 */
	public function edit(){


		//驾校id
		$school_id = $this->school_id;
		$id = I('get.id') ? I('get.id') : 0;//修改的主键id
		if(IS_POST){
			$site = M('site');
			$param = I('post.');
			$id = $param['id'];
			unset($param['id']);
			$site_data = $site->where('id = '.$id)->find();
			if($site_data){
				$res = $site->where(array('id'=>$id,'school_id'=>$school_id))->save($param);
				$this->redirect('Site/index');
			}else{
				$this->error('修改场地失败,参数错误！');
			}
		}else{
			  $site=M('site');
			  $data= $site->where('id='.$id)->find();
			  $this->assign('data',$data);
			  $this->display();
		}
	}



	//车辆删除
	public  function   del(){
		$id = I('get.id') ? I('get.id') : 0;
		$school_id = session('uid');
		//$school_id = 2; //驾校id
		$where['id'] = $id;
		$where['school_id'] = $school_id;
		$site = M('site');
		$site_data = $site->where($where)->find();

		if($site_data){
			//删除
			$status = $site_data['status'];
			if($status == 1 ){
				$param['status']=2;
				$res = $site->where(array('id'=>$id,'school_id'=>$school_id))->save($param);
				if($res){
					$this->success('删除场地信息，成功!');
				}else{
					$this->error('删除场地信息，失败!'.$site->getError());
				}
			}else{
				$this->error('多次操作！');
			}
		}else{
			$this->error('没有找到场地记录，信息删除失败!');
		}


	}



}